package com.curry.apiprovider.crawler.pageProcessor.job;

import com.alibaba.fastjson.JSON;
import com.curry.apiprovider.crawler.Constants;
import com.curry.livehelper.entity.domain.TJobInfo;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.model.OOSpider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 前程无忧的爬虫
 */
@Data
public class JobPageProcessor implements PageProcessor {

    private static final Site site = Site.me().setRetryTimes(3).setCharset("GB2312").setSleepTime(1000);
    private Logger logger = LoggerFactory.getLogger(JobPageProcessor.class);
    private String[] params;
    private String jobData;
    private List<TJobInfo> tJobInfos;

    public JobPageProcessor(String[] params) {
        this.params = params;
    }

    @Override
    public void process(Page page) {
        Html html = page.getHtml();
//        List<String> all1 = page.getHtml().css("div.dw_table").css("div.el").css("span.t2")
//                .$("a", "title").all();
        tJobInfos = new ArrayList<>();
        List<Selectable> selectableList = page.getHtml().css("div.dw_table").css("div.el").nodes();
        //职位名
        for (Selectable selectable : selectableList) {
            String jobPost = selectable.xpath("//span/a/text()").get();
            String companyName = selectable.xpath("//span[@class='t2']/a/text()").get();
            String area = selectable.xpath("//span[@class='t3']/text()").get();
            String salaryRange = selectable.xpath("//span[@class='t4']/text()").get();
            String link = selectable.css("p.t1").$("a", "href").get();
            if (!StringUtils.isEmpty(jobPost) && !StringUtils.isEmpty(link) && !StringUtils.isEmpty(companyName)) {
//                logger.warn("link " + link + "companyName " + companyName + " area " + area + " salaryRange " + salaryRange + " jobPost " + jobPost);
                TJobInfo tJobInfo = new TJobInfo();
                tJobInfo.setJobPost(jobPost);
                tJobInfo.setKeyWord(params[1]);
                tJobInfo.setCityCode(params[0]);
                tJobInfo.setCommpanyName(companyName);
                tJobInfo.setOfficeAddress(area);
                tJobInfo.setSalaryArea(salaryRange);
                JobPipLine jobPipLine = new JobPipLine();
                jobPipLine.setKeyType(Constants.KEY_JOB_INFO);
                OOSpider.create(new SubJobPageProcessor(tJobInfo)).addUrl(link)
                        .addPipeline(new ConsolePipeline())
                        .addPipeline(jobPipLine)
                        .thread(4).run();
                String data = jobPipLine.getData();
                TJobInfo resultJob = JSON.parseObject(data, TJobInfo.class);
                tJobInfos.add(resultJob);

            }
        }
        logger.warn("开始保存到临时文件");
        write2TempFile(JSON.toJSONString(tJobInfos));
        //link http://jobs.51job.com/shanghai/82292806.html?s=01&t=0
//        http://jobs.51job.com/all/co183.html
    }

    private void write2TempFile(String s) {
        File file = new File(Constants.FILE_TEMP_JOB);
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(s.getBytes());
            fileOutputStream.flush();
            logger.warn("保存到工作信息到临时文件");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    @Override
    public Site getSite() {
        return site;
    }
}
